﻿using System;
using System.Threading;

namespace ThreadDemo_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread thred = new Thread(()=> {
                Console.WriteLine(Thread.CurrentThread.Name);
                for (int i = 0; i < 1000; i++)
                {
                    //注意:当Thread.Sleep(0)时会导致线程立即放弃本身当前的时间片，自动将CPU移交给其他线程。
                    Thread.Sleep(2);

                    //同Thread.Sleep(0)，线程会立即放弃本身当前的时间片
                    //Thread.Yield();

                    Console.Write("- ");
                }
            });
            Thread.CurrentThread.Name = "主线程";
            thred.Name = "次线程";
            thred.Start();

            //阻塞
            if (thred.Join(2000))
            {
                Console.WriteLine("次线程执行结束");
            }
            else 
            {
                Console.WriteLine("次线程执行超时");
            }

            Console.WriteLine(Thread.CurrentThread.Name);
            for (int i = 0; i < 1000; i++)
            {
                Console.Write("* ");
            }
        }
    }
}
